package org.dromara.app.controller;

import java.util.List;

import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.app.domain.sys.SysSumData;
import org.dromara.app.domain.sys.SysSumReturn;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.app.domain.vo.SkAppUserVo;
import org.dromara.app.domain.bo.SkAppUserBo;
import org.dromara.app.service.ISkAppUserService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * app用户
 *
 * @author Evacuation
 * @date 2024-07-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/SkAppUser")
public class SkAppUserController extends BaseController {

    private final ISkAppUserService skAppUserService;

    /**
     * 查询app用户列表
     */
    @SaCheckPermission("app:SkAppUser:list")
    @GetMapping("/list")
    public TableDataInfo<SkAppUserVo> list(SkAppUserBo bo, PageQuery pageQuery) {

        return skAppUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出app用户列表
     */
    @SaCheckPermission("app:SkAppUser:export")
    @Log(title = "app用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SkAppUserBo bo, HttpServletResponse response) {
        List<SkAppUserVo> list = skAppUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "app用户", SkAppUserVo.class, response);
    }

    /**
     * 获取app用户详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("app:SkAppUser:query")
    @GetMapping("/{id}")
    public R<SkAppUserVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(skAppUserService.queryById(id));
    }

    /**
     * 新增app用户
     */
    @SaCheckPermission("app:SkAppUser:add")
    @Log(title = "app用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SkAppUserBo bo) {
        return toAjax(skAppUserService.insertByBo(bo));
    }

    /**
     * 修改app用户
     */
    @SaCheckPermission("app:SkAppUser:edit")
    @Log(title = "app用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SkAppUserBo bo) {
        return toAjax(skAppUserService.updateByBo(bo));
    }

    /**
     * 删除app用户
     *
     * @param ids 主键串
     */
    @SaCheckPermission("app:SkAppUser:remove")
    @Log(title = "app用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(skAppUserService.deleteWithValidByIds(List.of(ids), true));
    }


    @SaCheckPermission("app:SkAppUser:settinghhr")
    @Log(title = "app用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("updateSkAppUserSetting")
    public R<Void> updateSkAppUserSetting(@Validated(EditGroup.class) @RequestBody SkAppUserBo bo) {
        return toAjax(skAppUserService.updateSkAppUserSetting(bo));
    }

    /**
     * 获取用户统计信息
     */
    @GetMapping("/getSum")
    public R<SysSumReturn> getSum() {
        return R.ok(skAppUserService.getSum());
    }

     /**
     * 获取用户统计信息
     */
    @GetMapping("/getSumData")
    public R<SysSumData> getSumData(@Parameter(description = "查询开始时间") @RequestParam("startTime") String startTime,
                                    @Parameter(description = "查询结束时间") @RequestParam("endTime") String endTime) {
        return R.ok(skAppUserService.getSumData(startTime, endTime));
    }

}
